package com.xiaoyu.commonlib.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jx on  2018/4/2;
 * 添加fragment所用到的万能适配器
 */

public class CommonFragmentPagerAdapter extends FragmentPagerAdapter {

    List<Fragment> mFragments;
    protected String[] mTitles;


    public CommonFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.mFragments = fragments;
    }

    public CommonFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, String[] mTitles) {
        super(fm);
        if (fragmentList == null) {
            fragmentList = new ArrayList<>();
        }
        this.mFragments = fragmentList;
        this.mTitles = mTitles;
    }

    public void add(Fragment fragment) {
        if (isEmpty()) {
            mFragments = new ArrayList<>();

        }
        mFragments.add(fragment);
    }

    @Override
    public int getCount() {
        return isEmpty() ? 0 : mFragments.size();
    }

    @Override
    public Fragment getItem(int arg0) {
        return isEmpty() ? null : mFragments.get(arg0);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }

    public void updateList(List<Fragment> list) {
        this.mFragments = list;
        this.notifyDataSetChanged();
    }

    public boolean isEmpty() {
        return mFragments == null;

    }


}
